﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLLUtilidades;

namespace BLLMisIdeas
{
    public class BLLIdeas:BLLPadre<MisIdeasDataContext>
    {
        public BLLIdeas(MisIdeasDataContext contexto) : base(contexto) { }
        public BLLIdeas() : this(new MisIdeasDataContext()) { }

        public List<MI_Idea> TraerIdeas()
        {
            List<MI_Idea> lista = (from i in Contexto.MI_Ideas
                                    where i.MI_IdeaDetalles.Count() > 0
                                    select i).ToList();
            lista = (from i in lista
                      orderby i.MI_IdeaDetalles.OrderByDescending(d => d.MI_Fecha).FirstOrDefault().MI_Fecha descending
                      select i).ToList();

            List<MI_Idea> pija = Contexto.MI_Ideas.Where(i => i.MI_IdeaDetalles.Count() == 0).OrderBy(i => i.MI_Nombre).ToList();
            lista.AddRange(pija);

            return lista;
        }

        public MI_Idea TraerIdea(int ideaID)
        {
            return (from i in Contexto.MI_Ideas
                    where i.MI_IdeaID == ideaID
                    select i).FirstOrDefault();
        }

        public void CargarIdea(MI_Idea idea)
        {
            Contexto.MI_Ideas.InsertOnSubmit(idea);
            Contexto.SubmitChanges();
        }

        public void ModificarIdea(MI_Idea idea)
        {
            MI_Idea ideaVieja = TraerIdea(idea.MI_IdeaID);

            ideaVieja.MI_Nombre = idea.MI_Nombre;
            ideaVieja.MI_Objetivos = idea.MI_Objetivos;

            Contexto.SubmitChanges();
        }

        public void EliminarIdea(MI_Idea idea)
        {
            MI_Idea ideaVieja = TraerIdea(idea.MI_IdeaID);
            Contexto.MI_Ideas.DeleteOnSubmit(ideaVieja);
            Contexto.SubmitChanges();
        }
    }
}
